<template>
  <p class="g-name ellipsis">{{ goods.name }}</p>
  <p class="g-desc ellipsis">{{ goods.desc }}</p>
  <p class="g-price">
    <span><i>¥</i>{{ goods.price }}</span>
    <span>¥{{ goods.oldPrice }}</span>
  </p>

  <div class="g-service">
    <dl>
      <dt>促销</dt>
      <dd>12月好物发送, APP领劵购买直降120元</dd>
    </dl>
    <dl>
      <dt>配送</dt>
      <dd>至
        <XtxCity :fullLocation="fullLocation" @change="changeAddress"></XtxCity>
      </dd>
    </dl>
    <dl>
      <dt>服务</dt>
      <dd>
        <span>无忧退货</span>
        <span>快速退款</span>
        <span>免费包邮</span>
        <a href="javascript:;">了解更多</a>
      </dd>
    </dl>
  </div>
</template>

<script>


import { ref } from 'vue'

export default {
  name: 'GoodsName',
  props: {
    goods: {
      type: Object,
      default: () => {
      }
    }
  },
  setup(props) {
    // 提供给后台的四项数据(没登录默认情况)
    const provinceCode = ref('110000')
    const cityCode = ref('119900')
    const countyCode = ref('110101')
    const fullLocation = ref('北京市 市辖区 东城区')

    // 如果用户有登录, 取出用户收货地址中默认的地址给是个数据赋值 (已登录)
    if (props.goods.useAddresses) {
      const defaultAddress = props.goods.useAddresses.find(p => p.isDefault === 1)
      if (defaultAddress) {
        provinceCode.value = defaultAddress.provinceCode
        cityCode.value = defaultAddress.cityCode
        countyCode.value = defaultAddress.countyCode
        fullLocation.value = defaultAddress.fullLocation
      }
    }
    const changeAddress = (res) => {
      fullLocation.value = res.fullLocation
      provinceCode.value = res.provinceCode
      cityCode.value = res.cityCode
      countyCode.value = res.countyCode
    }

    return {
      fullLocation,
      changeAddress
    }
  }
}
</script>

<style lang="less" scoped>
.g-name {
  font-size: 22px;
  color: #333;
  //line-height: 30;
}

.g-desc {
  margin-top: 10px;
  font-size: 14px;
  color: #999;
}

.g-price {
  margin-top: 10px;

  span {
    &:first-child {
      i {
        font-size: 14px;
      }

      margin-right: 10px;
      color: @priceColor;
      font-size: 22px;
    }

    &:last-child {
      color: #999;
      text-decoration: line-through;
      font-size: 16px;
    }
  }
}

.g-service {
  margin-top: 10px;
  padding: 20px 10px 0 10px;
  width: 500px;
  background: #f5f5f5;

  dl {
    display: flex;
    padding-bottom: 20px;
    align-items: center;

    dt {
      color: #999;
      width: 50px;
    }

    dd {
      color: #666;

      &:last-child {
        span {
          margin-right: 10px;

          &::before {
            content: '•';
            color: @xtxColor;
            margin-right: 2px;
          }
        }

        a {
          color: @xtxColor;
        }
      }
    }
  }
}
</style>
